package com.hst.capacity.domain.service.impl;

import cn.smartravel.traffic.usoft17.RideSupplierCallback;
import cn.smartravel.traffic.usoft17.model.request.callback.OrderStatusDTO;
import cn.smartravel.traffic.usoft17.model.response.callback.ResultVO;
import com.alibaba.fastjson2.JSON;
import com.hst.capacity.application.biz.DingTalkBiz;
import com.hst.capacity.application.biz.OrderBiz;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.common.constants.TongChengConstants;
import com.hst.capacity.domain.model.request.OrderParam;
import com.hst.capacity.domain.model.request.dispatch.DispatchParam;
import com.hst.capacity.domain.model.request.elong.order.ElongOrderParam;
import com.hst.capacity.domain.model.response.driver.DriverInfoVO;
import com.hst.capacity.domain.model.response.order.OrderVO;
import com.hst.capacity.domain.repository.redis.CacheRedis;
import com.hst.capacity.domain.service.ExpressService;
import com.hst.capacity.domain.service.IntercityService;
import com.hst.capacity.domain.service.TcELongService;
import com.hst.capacity.infra.convertor.DingTalkConvertor;
import com.hst.capacity.infra.convertor.DriverConvertor;
import com.hst.capacity.infra.convertor.OrderConvertor;
import com.hst.capacity.infra.external.dingtalk.model.request.order.NewOrderRemindParam;
import com.hst.capacity.infra.util.StringUtil;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class TcELongServiceImpl implements TcELongService {

    @Autowired
    private RideSupplierCallback callback;

    @Autowired
    private CacheRedis cacheRedis;

    @Autowired
    private ExpressService exOrderService;

    @Autowired
    private IntercityService icOrderService;

    @Autowired
    OrderBiz orderBiz;

    @Autowired
    DingTalkBiz dingTalkBiz;

    @Override
    public void virtualAcceptOrder(ElongOrderParam param) {

        log.info("TcELongService virtualAcceptOrder");

        int maxNum = 10000;
//        if (StringUtil.isNotBlank(param.getIdLine())) {
//            maxNum = cacheRedis.getLineGrabMaxNumValue(param.getIdLine());
//            if (maxNum == -2)
//                maxNum = 10;
//        }

//        if (maxNum > 0) {
            OrderStatusDTO dto = new OrderStatusDTO()
                    .setStatus(TongChengConstants.ELong_OrderType.get(param.getStatus()))
                    .setDriverName(param.getDriverName())
                    .setDriverPhone(param.getDriverMobile())
                    .setVehiclePlateNum(param.getPlateNumber())
                    .setVehicleColor(param.getVehicleColor())
                    .setVehicleModel(param.getVehicleModel())
                    .setVehicleBrand(param.getVehicleBrand());

            dto.setOrderId(param.getExternalOrderId());
            dto.setSupplierOrderId(param.getIdOrder());

            DriverInfoVO driver = DriverConvertor.INSTANCE.convert2Info(param);
            cacheRedis.writeOrderDriverValue(param.getIdOrder(), JSON.toJSONString(driver));

            ResultVO resultVO = callback.orderStatus(param.getTenant(), dto);
            //log.info("resultVO:" + JSON.toJSONString(resultVO));
            if (resultVO.getStatus() == 200) {

                //log.info("ElongOrderParam: " + JSON.toJSONString(param));
                int moduletype = StringUtil.orderType(param.getIdOrder());
                OrderVO vo = null;
                OrderParam orderParam = OrderConvertor.INSTANCE.convert2Param(param);
                orderParam.setModuleType(moduletype);
                if (moduletype == Constants.OrderModuleType.EXPRESS) {
                    vo = exOrderService.virtualAcceptOrder(param.getIdOrder(), param.getIdDriver());
                } else if (moduletype == Constants.OrderModuleType.INTERCITY) {
                    //log.info("执行intercity虚拟接单");
                    vo = icOrderService.virtualAcceptOrder(param.getIdOrder(), param.getIdDriver());
                } else if (moduletype == Constants.OrderModuleType.SPECIAL) {
//                if (param.getIdServiceType() == 1
//                        || param.getIdServiceType() == 2
//                        || param.getIdServiceType() == 3
//                        || param.getIdServiceType() == 4) {
//                    count = orderPoolService.orderGrab(param);
//                } else {
//
//                }
                }

                try {
                    if (vo != null) {
                        if (vo.getIdFranchisee().equals("af4ad2044ec011ee9d84fa163e7d949d")) {
                            NewOrderRemindParam newOrderRemindParam = DingTalkConvertor.INSTANCE.convert(vo);
                            newOrderRemindParam.setChannel(Integer.parseInt(Constants.Channel.TONGCHENG_RIDE));
                            newOrderRemindParam.setBizType(moduletype);
                            dingTalkBiz.sendNewOrderRemind(newOrderRemindParam);
                        }
                    }
                } catch (ApiException e) {
                    log.info("钉钉机器人报错："+ e.getMessage());
                } catch (Exception e) {
                    log.info("钉钉机器人报错："+ e.getMessage());
                }

//                if (StringUtil.isNotBlank(param.getIdLine())) {
//                    cacheRedis.writeLineGrabMaxNumValue(param.getIdLine(), maxNum - 1);
//                }
                //lockRedis.clearValue(param.getIdOrder());
                //throw new BusinessException(5002, "同程接口回调抢单失败，请重新执行");
            }
//        }
    }

}
